#!/bin/sh

### vars
ori_file=
wrk_file=
ret_file=
pflag=false


### funs
echo_usage()
{
  echo "`basename $0` -f <spec_file> [-p]"
  echo "    -f        the spec file to format"
  echo "    -p        prepare process, used for first time"
}

#####################################################################
# add_zero
# args:
#   $1: file to process
#   $2: prefix string
# globals: none
# returns: none
#####################################################################
add_zero()
{
  target_file=$1
  prefix=$2
  local old_list=${prefix}.ol
  local new_list=${prefix}.nl
  local cat_list=${prefix}.cl

  [ -z "${target_file}" -o -z "${prefix}" ] && return

  cat ${target_file} | awk -F: '{print $1}' | grep ${prefix} | grep -v ${prefix}.... > ${old_list}
  cat ${old_list} | awk -F${prefix} '{printf "'"${prefix}"'%04d\n",$2}' > ${new_list}
  paste ${old_list} ${new_list} > ${cat_list}

  while read line; do
    oldkey=`echo ${line} | awk '{print $1}'`
    newkey=`echo ${line} | awk '{print $2}'`

    sed -i 's/'"${oldkey}:"'/'"${newkey}:"'/g' ${target_file}
  done < ${cat_list}

  rm -f ${old_list} ${new_list} ${cat_list}
}

#####################################################################
# do_prepare
# args: none
# globals:
#   wrk_file: will be changed
# returns: none
#####################################################################
do_prepare()
{
  echo "================Begin prepare process"
  cat ${ori_file} > ${ori_file}.pre
  local pre_file=${ori_file}.pre

  # delete comment
  sed -i '/^#/d' ${pre_file}
  sed -i '1i###byeX012: DO NOT DELETE THIS LINE' ${pre_file}

  # numberic, use same length
  add_zero ${pre_file} Source
  add_zero ${pre_file} Patch
  
  # alignment, key and indent use 16 chars
  # Name:            name
  # Version:         version
  # Release:         release
  # ...
  sed -i 's/Name: */Name:           /g' ${pre_file}
  sed -i 's/Version: */Version:        /g' ${pre_file}
  sed -i 's/Release: */Release:        /g' ${pre_file}
  sed -i 's/Epoch: */Epoch:          /g' ${pre_file}
  sed -i 's/Summary: */Summary:        /g' ${pre_file}
  sed -i '/Group: */d' ${pre_file}
  sed -i 's/License: */License:        /g' ${pre_file}
  sed -i 's/URL: */URL:            /g' ${pre_file}
  sed -i 's/\(Source....\): */\1:     /g' ${pre_file}
  sed -i 's/\(Patch....\): */\1:      /g' ${pre_file}
  sed -i 's/BuildArch: */BuildArch:      /g' ${pre_file}
  sed -i 's/Requires: */Requires:       /g' ${pre_file}
  sed -i 's/BuildRequires: */BuildRequires:  /g' ${pre_file}
  sed -i 's/Provides: */Provides:       /g' ${pre_file}
  sed -i 's/Obsoletes: */Obsoletes:      /g' ${pre_file}
  sed -i 's/Conflicts: */Conflicts:      /g' ${pre_file}
  sed -i 's/AutoReqProv: */AutoReqProv:    /g' ${pre_file}

  wrk_file=${pre_file}
  echo "================End prepare process"
}

#####################################################################
# do_format
# args: none
# globals: none
# returns: none
#####################################################################
do_format()
{
  echo "================Begin format process"
  if [ -f ${wrk_file} ]; then
    cat ${wrk_file} > ${ret_file}
  else
    cat ${ori_file} > ${ret_file}
  fi

  # combination, line less than 55 chars does not wrap
  sed -i '/^Requires:.\{1,46\}$/{N;s/\nRequires: */ /g}' ${ret_file}
  sed -i '/^BuildRequires:.\{1,41\}$/{N;s/\nBuildRequires: */ /g}' ${ret_file}
  sed -i '/^Provides:.\{1,46\}$/{N;s/\nProvides: */ /g}' ${ret_file}
  sed -i '/^Obsoletes:.\{1,45\}$/{N;s/\nObsoletes: */ /g}' ${ret_file}
  sed -i '/^Conflicts:.\{1,45\}$/{N;s/\nConflicts: */ /g}' ${ret_file}
  
  echo "================End format process"
}

echo_result()
{
  diff ${ori_file} ${ret_file} >/dev/null 2>&1

  if [ $? -ne 0 ]; then
    echo "spec file changed, please check ${ret_file}"
    rm -f ${wrk_file}
  else
    # should never go here
    echo "it seems nothing need to format!"
    rm -f ${wrk_file} ${ret_file}
    exit 3
  fi
}


### main
while getopts "f:p" arg
do
  case $arg in
  f)
    ori_file=$OPTARG
    wrk_file=${ori_file}.wrk
    ret_file=${ori_file}.fmt
    ;;
  p)
    pflag=true
    ;;
  ?)
    echo_usage
    exit 1
    ;;
  esac
done

if [ -z "${ori_file}" ]; then
  echo_usage
  exit 1
fi

if [ ! -f ${ori_file} ]; then
  echo "${ori_file} not exists"
  exit 2
fi

if [ "true" = ${pflag} ]; then
  do_prepare
fi

do_format

echo_result

exit 0
